package com.zahb.qadx.ui.activity.videopage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.model.JsonRootMicroVideoBean;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/*微视频列表
2021.3.12
 */

public class MicroVideoListActivity extends BaseActivity {

    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager2)
    ViewPager2 viewPager2;
    @BindView(R.id.search)
    RelativeLayout search;
    @BindView(R.id.return_to_cancel)
    ImageView returnToCancel;
    private String[] mTitles;
    private Fragment[] mFragments;
    static JsonRootMicroVideoBean classifiedListDataBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTopBarContainer.setVisibility(View.GONE);
        classifiedListDataBean = (JsonRootMicroVideoBean) getIntent().getSerializableExtra("jsonRootMicroVideoBean");

        classifiedListData();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_micro_video_list;
    }

    @Override
    protected void initViews() {
//        classifiedListData();
        mUnbinder = ButterKnife.bind(this);

    }

    //微视频分类
    private void classifiedListData() {
        mTitles = new String[classifiedListDataBean.getData().size()];
        if (classifiedListDataBean != null) {
            for (int i = 0; i < classifiedListDataBean.getData().size(); i++) {
                mTitles[i] = classifiedListDataBean.getData().get(i).getName();
            }
            mFragments = new Fragment[mTitles.length];
            viewPager2.setAdapter(new TaskListFragmentAdapter(this, mFragments));
            viewPager2.setOffscreenPageLimit(mFragments.length - 1);
            TabLayoutMediator mediator = new TabLayoutMediator(tabLayout, viewPager2,
                    (tab, position) -> tab.setText(mTitles[position]));
            mediator.attach();
        }

    }

    @Override
    protected int getTitleStringRes() {
        return R.string.micro_video_list;
    }

    @OnClick({R.id.return_to_cancel, R.id.search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.return_to_cancel://返回
                finish();
                break;
            case R.id.search://搜索
            SearchActivity.actionStart(getContext(), classifiedListDataBean);
            getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                break;
        }
    }


    private static class TaskListFragmentAdapter extends FragmentStateAdapter {

        private Fragment[] mFragments;


        TaskListFragmentAdapter(@NonNull FragmentActivity fragmentActivity, Fragment[] fragments) {
            super(fragmentActivity);
            mFragments = fragments;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            if (mFragments[position] == null) {
                mFragments[position] = TitlevideoFragment.newInstance(position + 1, classifiedListDataBean.getData().get(position).getId());
            }
            return mFragments[position];
        }

        @Override
        public int getItemCount() {
            return mFragments.length;
        }
    }

    public static void actionStart(Context context, JsonRootMicroVideoBean jsonRootMicroVideoBean) {
        Intent intent = new Intent(context, MicroVideoListActivity.class);
        Bundle args = new Bundle();
        args.putSerializable("jsonRootMicroVideoBean", jsonRootMicroVideoBean);
        intent.putExtras(args);
        context.startActivity(intent);
    }
}
